package com.example.computerstroe.service.impl;

import com.example.computerstroe.Tool.Tool;
import com.example.computerstroe.entity.User;
import com.example.computerstroe.mapper.UserMapper;
import com.example.computerstroe.service.IUserService;
import com.example.computerstroe.service.ex.InsertException;
import com.example.computerstroe.service.ex.UserNameDuplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service("UserServiceImpl")
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private Tool tool;
    @Override
    public void insert(User user) {
        User result=userMapper.findByUserName(user.getUsername());
        if(result!=null) {
            throw new UserNameDuplicationException("用户名被占用");
        }else{
            user.setIsDelete(0);
            Date date=new Date();
            user.setCreatedTime(date);
            user.setModifiedTime(date);
            user.setSalt(UUID.randomUUID().toString().toUpperCase());
            user.setPassword(tool.MD5(user.getSalt(),user.getPassword()));
            Integer count=userMapper.insert(user);
            if(count!=1){
                throw  new InsertException("创建失败");
            }
        }
    }

    @Override
    public User findByUserName(String username) {
        return null;
    }
}
